package com.blb.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.blb.entity.User;
import com.blb.entity.UserExample;
import com.blb.entity.UserExample.Criteria;
import com.blb.mapper.UserMapper;
import com.blb.service.UserService;
import com.blb.utils.DateUtils;
import com.blb.utils.LayuiPage;
import com.blb.utils.MybatisUtils;
import com.blb.utils.UUIDUtils;
import com.mysql.jdbc.StringUtils;

@Controller
@RequestMapping("user")
public class UserController {

	private static String filePath = "D:/file_upload/";
	private UserService userService = new UserService();
	
	
	/**
	 * 用户登录
	 * @param user
	 * @param session
	 * @param model
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("login")
	public String sadas(String username, String password, HttpSession httpSession, RedirectAttributes model) throws IOException{
		String msg = userService.login(new User(username, password));
		
		SqlSession session = MybatisUtils.getSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();
		criteria.andUsernameEqualTo(username);
		
		List<User> list = userMapper.selectByExample(userExample);
		
		if(!msg.equals("")){
			model.addFlashAttribute("msg", msg);
			return "redirect:/loginPage";
		}
		httpSession.setAttribute("user", list.get(0));
		return "index";
	}
	
	/**
	 * 分页
	 * @param page
	 * @param limit
	 * @param username
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("page")
	public LayuiPage page(int page,int limit,String username) throws IOException{
		LayuiPage pagelist = userService.page((page-1)*limit, limit, username);
		return pagelist;
	}
	
	/**
	 * 显示图片
	 * @param name
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("getImage")
	public ResponseEntity<byte[]> getImage(String name) throws IOException{
		File file = new File(filePath + name);
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.setContentType(MediaType.IMAGE_JPEG);
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), httpHeaders, HttpStatus.OK);
	}
	
	
	/**
	 * 判断用户名是否存在
	 * @param username
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("isExist")
	public String isExist(String username) throws IOException{
		List<User> list = userService.isExist(username);
		if(!list.isEmpty()){
			return "用户名已存在";
		}
		return "";
	}
	
	
	/**
	 * 添加
	 * @param user
	 * @param httpsession
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("add")
	public String add(User user,HttpSession httpsession) throws IOException{
		user.setId(UUIDUtils.getID());
		user.setCreateTime(DateUtils.gettime());
		
		User user1 = (User) httpsession.getAttribute("user");
		user.setCreateUser(user1.getUsername());
		try {
			userService.add(user);
		} catch (IOException e) {
			e.printStackTrace();
			return "添加失败"+e.getMessage();
		}
		return "";
	}
	
	 @InitBinder
	    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
	        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	        dateFormat.setLenient(false);
	        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	    }
	
	/**
	 * 修改
	 * @param user
	 * @param httpsession
	 * @return
	 */
	@ResponseBody
	@RequestMapping("update")
	public String update(User user,HttpSession httpsession){
		user.setState(user.getState()==null?-1:1);
		user.setUpdateTime(DateUtils.gettime());
		
		User user1 = (User) httpsession.getAttribute("user");
		user.setUpdateUser(user1.getUsername());
		
		
		try {
			userService.update(user);
		} catch (IOException e) {
			e.printStackTrace();
			return "修改失败"+e.getMessage();
		}
		return "";
	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete(@RequestBody String[] ids){
		try {
			userService.delete(ids);
		} catch (IOException e) {
			e.printStackTrace();
			return e.getMessage();
		}
		return "";
	}
}
